package com.xie.controller;

import com.sun.org.apache.xpath.internal.operations.Mod;
import com.xie.pojo.Results;
import com.xie.service.ResultsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/results")
public class ResultsController {

    @Autowired
    @Qualifier("ResultsServiceImpl")
    private ResultsService resultsService;

    @RequestMapping("/allResults")
    public String allResults(HttpSession session){
        List<Results> results = resultsService.queryAllResults();
        session.setAttribute("resultsList",results);
        return "results";
    }

    @RequestMapping("/deleteResults")
    public String deleteResults(String cname, Model model){
        int i = resultsService.deleteResults(cname);
        if(i > 0){
            return "redirect:/results/allResults";
        }else{
            model.addAttribute("msg","删除失败");
            return "redirect:/results/allResults";
        }
    }

    @RequestMapping("/toAddResults")
    public String toAddResults(){
        return "addResults";
    }
    @RequestMapping("/addResults")
    public String addResults(Results results,Model model){
        int i = resultsService.addResults(results);
        if (i > 0){
            return "redirect:/results/allResults";
        }else{
            model.addAttribute("msg","添加失败");
            return "redirect:/results/toAddResults";
        }
    }

    @RequestMapping("/toUpdateResults")
    public String toUpdateResults(String cname, Model model){
        Results results = resultsService.queryResults(cname);
        model.addAttribute("QResults",results);
        return "updateResults";
    }
    @RequestMapping("/updateResults")
    public String updateResults(Results results,Model model){
        int i = resultsService.updateResults(results);
        if (i > 0){
            return "redirect:/results/allResults";
        }else{
            model.addAttribute("msg","修改失败");
            return "redirect:/results/allResults";
        }
    }






}
